﻿#include "cyxvalueeditplugin.h"

CYxValueEditPlugin::CYxValueEditPlugin(QWidget *parent) : CTableEditPlugin(parent) {
    QHBoxLayout* titleLayout = new QHBoxLayout;
    m_addbtn = new QPushButton("添加", this);
    m_delbtn = new QPushButton("删除选中", this);
    titleLayout->addStretch(1);
    titleLayout->addWidget(m_delbtn);
    titleLayout->addWidget(m_addbtn);

    mainLayout = new QVBoxLayout;
    mainLayout->addLayout(titleLayout);
    mainLayout->addStretch(1);

    this->setWindowTitle("编辑遥信值映射");
    this->setLayout(mainLayout);

    connect(m_addbtn, &QPushButton::clicked, this, [&](){
        int v = -1;
        QString desc("我是值描述");
        if(m_items.size()) {
            for(auto ptr : m_items) {
                if(ptr->value >= v) {
                    v = ptr->value;
                    desc =  ptr->descr;
                }
            }
        }

        UiValueMapEdit* editbox = new UiValueMapEdit(v + 1, desc, CPlatIcon());
        m_items.insert(editbox, editbox);
        mainLayout->insertWidget(mainLayout->count()-1, editbox);
    });

    connect(m_delbtn, &QPushButton::clicked, this, [&](){
        QList<UiValueMapEdit*> dels;

        for(UiValueMapEdit* item : m_items.keys()) {
            if(item->checkState())
                dels.append(item);
        }

        for(UiValueMapEdit* item : dels) {
            m_items.remove(item);
            mainLayout->removeWidget(item);
            mainLayout->update();
            delete item;
        }
    });
}

bool CYxValueEditPlugin::parseData(QString data)
{
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data.toUtf8());
    if (jsonDoc.isNull() || !jsonDoc.isObject())
        return false;

    root = jsonDoc.object();

    QJsonArray arr = root["valuesmap"].toArray();
    for (int i = 0; i < arr.size(); ++i) {
        QJsonValue value = arr.at(i);
        if (value.isObject()) {
            QJsonObject obj = value.toObject();
            int v = obj["value"].toInt();
            QString desc = obj["v_descr"].toString();
            QJsonObject jsonicon = obj["v_image"].toObject();
            CPlatIcon icon;
            icon.fromJson(jsonicon);

            UiValueMapEdit* editbox = new UiValueMapEdit(v, desc, icon);
            m_items.insert(editbox, editbox);
            mainLayout->insertWidget(mainLayout->count()-1, editbox);
        }
    }
    return true;
}

QString CYxValueEditPlugin::readData()
{
    QJsonObject resobj;
    QJsonArray arr;
    for(const auto& it : m_items){
        QJsonObject obj;
        obj["value"] = it->value;
        obj["v_descr"] = it->descr;
        obj["v_image"] = it->iconpath.toJson();
        arr.append(obj);
    }
    resobj["valuesmap"] = arr;
    return QJsonDocument(resobj).toJson(QJsonDocument::Compact);
}

bool CYxValueEditPlugin::checkData() {
    QSet<int> values;
    for(const auto& it : m_items) {
        if(it->descr.isEmpty())
            return false;

        if(values.contains(it->value))
            return false;

        values.insert(it->value);
    }
    return true;
}

int CYxValueEditPlugin::vcount() {
    return m_items.size();
}
